package org.jxc.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.dao.SupplierDAO;
import org.jxc.domain.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.domain.Department;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.LoginBean;
import org.team4.util.Validate;
import org.team4.vo.Page;

@Service
public class SupplierService implements IService {

	private SupplierDAO supplierDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		supplierDAO = (SupplierDAO) DAOFactory.getDAO(sessionFactory,
				SupplierDAO.class);
	}

	public Page<Supplier> listPagionation(Page<Supplier> page)
			throws CRUDException {
		return supplierDAO.pagination(page,
				"from Supplier where deleteFlag='0'", null);
	}

	public List<Supplier> findAll() throws CRUDException {
		return supplierDAO.findAll();
	}

	public Supplier findById(String supplierId) throws CRUDException {
		return supplierDAO.get(supplierId);
	}

	public void saveSupplier(Supplier supplier) throws CRUDException {
		supplier.setDeleteFlag("0");
		supplier.setCreator(LoginBean.getLoginBean().getUser());
		supplier.setCreateTime(new Date());
		supplierDAO.save(supplier);
	}

	public void updateSupplier(Supplier supplier) throws CRUDException {
		Supplier oldSupplier = supplierDAO.get(supplier.getId());
		oldSupplier.setAddress(supplier.getAddress());
		oldSupplier.setCode(supplier.getCode());
		oldSupplier.setEmail(supplier.getEmail());
		oldSupplier.setHelpCode(supplier.getHelpCode());
		oldSupplier.setLinkman(supplier.getLinkman());
		oldSupplier.setMobile(supplier.getMobile());
		oldSupplier.setName(supplier.getName());
		oldSupplier.setOperator(supplier.getOperator());
		oldSupplier.setPhone(supplier.getPhone());
		oldSupplier.setRegion(supplier.getRegion());
		oldSupplier.setRemark(supplier.getRemark());
		oldSupplier.setSendAddress(supplier.getSendAddress());
		oldSupplier.setType(supplier.getType());
		oldSupplier.setFax(supplier.getFax());
		oldSupplier.setEditor(LoginBean.getLoginBean().getUser());
		oldSupplier.setEditTime(new Date());
		supplierDAO.update(oldSupplier);
	}

	public List<Supplier> findSupplierByIds(String ids) throws CRUDException {
		List<Supplier> sList = new ArrayList<Supplier>();
		String[] s = ids.split(",");
		for (int i = 0; i < s.length; i++) {
			if (Validate.notNull(s[i])) {
				Supplier su = supplierDAO.get(s[i]);
				sList.add(su);
			}
		}
		return sList;
	}

	/**
	 * 批量删除供应商
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public void deleteSupplier(String id) throws CRUDException {
		Supplier supplier = supplierDAO.get(id);
		supplier.setDeleteFlag("1");
		supplierDAO.update(supplier);
	}
}
